<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css"
	rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js"
	type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js"
	type="text/javascript"></script>
<script type="text/javascript">
	function sosuo() {
		var lpath = location.pathname;
		$("#soform").attr("action", lpath + "?pageNo=" + 1);

		$("#soform").submit();
	}
</script>
</head>

<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading">
			<button style="float: right" type="button" onclick="add()"
				class="btn btn-info">
				<span class="glyphicon glyphicon-plus"></span>
				&nbsp;新增系统参数
			</button>
			<form id="soform" method="post">
				<table>
					<tr>
						<td class="w80" style="padding-top: 5px">
							<label for="companyname" class="center-block">参数名称：</label>
						</td>
						<td class="w150 ">
							<input type="text" class="form-control" name="so_name"
								value="${so_name!}" id="so_name" placeholder="参数名称"
								data-options="">
						</td>

						<td style="padding-left: 10px">
							<div class="input-group">
								<span class="input-group-btn">
									<button class="btn btn-default" type="button" onclick="sosuo()">搜索</button>
								</span>
							</div>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<table
			class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th width="5">排序</th>
					<th width="20%">参数名称</th>
					<th width="20%">描述</th>
					<th width="20%">编码</th>
					<th width="20%">值</th>
					<th width="15%">操作</th>
				</tr>
			</thead>
			[#list pagination.list as config ]
			<tr>
				<td align="center">${config_index + 1}</td>
				<td align="center">${config.name!}</td>
				<td align="center">${config.descr!}</td>
				<td align="center">${config.code!}</td>
				<td align="center" id="value${config_index}">${config.value!}</td>
				<td align="center">
					<button type="button" class="btn btn-danger btn-xs delbtn"
						onclick="edit('${config.id}','${config.name!}','${config.descr!}','${config.code!}','${config.value!}','${config.valueSets!}')">编辑</button>
					<button type="button" class="btn btn-danger btn-xs delbtn"
						onclick="del('${config.id}')">删除</button>
				</td>
			</tr>
			[/#list]
		</table>

		<div class="panel-footer">[@fenye p = pagination form =
			'infoForm' /]</div>
	</div>


	<!-- 新增的弹出页 -->
	<div id="addconfig" style="width: 450px; height: 300px; display: none;">
		<form id="addform" method="post" class="form-horizontal" role="form">

			<div class="form-group">
				<label class="col-sm-3 control-label" id="clickforpro">参数名称</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入参数名称"
						min="1" required="required" name="name" id="addname">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">描述</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入描述" min="1"
						required="required" name="descr" id="adddescr">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">编码</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入编码" min="1"
						required="required" name="code" id="addcode">
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">值集</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="用分号隔开" min="1"
						required="required" name="valueSets" id="addvaluesets"
						onchange="setaddval(this)">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">值</label>
				<div class="col-sm-7" id="addvalues" style="margin-top:5px">
					<p style="margin-top: 2px;color: red;">请先完善参数值集</p>
				</div>
			</div>
			<div class="form-group" style="margin-top: 15px;">
				<div class="col-sm-offset-3 col-sm-10">
					<input id="addsubmit" onclick="subadd()" type="button"
						class="btn btn-default" value="提交" />
				</div>
			</div>
		</form>
	</div>

	<!-- 编辑的弹出页 -->
	<div id="editconfig"
		style="width: 450px; height: 300px; display: none;">
		<form id="editform" method="post" class="form-horizontal" role="form">
			<input type="hidden" name="id" id="editid">
			<div class="form-group">
				<label class="col-sm-3 control-label" id="clickforpro">参数名称</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入参数名称"
						min="1" required="required" name="name" id="editname">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">描述</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入描述" min="1"
						required="required" name="descr" id="editdescr">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">编码</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="请输入编码" min="1"
						required="required" name="code" id="editcode">
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">值集</label>
				<div class="col-sm-7">
					<input type="text" class="form-control" placeholder="用分号隔开" min="1"
						required="required" name="valueSets" id="editvaluesets" onchange="seteditval(this,'')">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">值</label>
				<div class="col-sm-7" id="editvalues" style="margin-top:5px">
				</div>
			</div>
			<div class="form-group" style="margin-top: 15px;">
				<div class="col-sm-offset-3 col-sm-10">
					<input id="editsubmit" onclick="subedit()" type="button"
						class="btn btn-default" value="提交" />
				</div>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	function setaddval(obj){
		if(obj.value == ''){
			$("#addvalues").html("<p style='margin-top: 2px;color: red;'>请先完善参数值集</p>");
			return;
		}
		var values = obj.value.split(";");
		var htmltext = "";
		for (var i=0;i<values.length;i++){
			var value = values[i];
			htmltext += value+"<input type='radio' name='value' value="+value+">&nbsp&nbsp&nbsp";
		}
		$("#addvalues").html(htmltext);
	}
	function seteditval(obj,choosenvalue){
		if(obj.value == ''){
			$("#editvalues").html("<p style='margin-top: 2px;color: red;'>请先完善参数值集</p>");
			return;
		}
		var values = obj.value.split(";");
		var htmltext = "";
		for (var i=0;i<values.length;i++){
			var value = values[i];
			if(value==choosenvalue){
				htmltext += value+"<input type='radio' checked='checked' name='value' value="+value+">&nbsp&nbsp&nbsp";
			}else{
				htmltext += value+"<input type='radio' name='value' value="+value+">&nbsp&nbsp&nbsp";
			}
		}
		$("#editvalues").html(htmltext);
	}




	//展示新增窗口
	function add() {
		$("#editvalues").html("<p style='margin-top: 2px;color: red;'>请先完善参数值集</p>");
		$("#editvaluesets").val('');
		dialog = art.dialog({
			title : "新增系统配置",
			lock : true,
			content : document.getElementById('addconfig')
		});
	}
	//删除
	function del(id) {//弹出警告框判断是否删除
		if (confirm('是否确认删除')) {
			$.ajax({
				url : '/config/ajax_del.do',
				type : 'get',
				dataType : 'json',
				data : {
					id : id
				},
				beforeSend : function() {
					art.dialog.tips("删除中...");
				},
				success : function(data) {
					if (data.success) {
						location.reload();
					} else {
						error_msg(data.msg, function() {
							location.reload();
						});
					}
				}
			});
		}
	}
	//打开编辑窗口
	function edit(id, name, descr, code, value, valueSets) {
		$("#addvalues").html("<p style='margin-top: 2px;color: red;'>请先完善参数值集</p>");
		$("#addvaluesets").val('');
		$("#editid").val(id);
		$("#editname").val(name);
		$("#editdescr").val(descr);
		$("#editcode").val(code);
		$("#editvaluesets").val(valueSets);
		var obj= document.getElementById('editvaluesets');
		seteditval(obj,value);

		dialog = art.dialog({
			title : "修改系统配置",
			lock : true,
			content : document.getElementById('editconfig')
		});
	}
	//新增提交
	function subadd(){
	[#--提交之前 form 要手动进行验证--]
	
	var name = $("input[id='addname']").val();
	if (!name) {
		alert('参数名称必须填写!');
		return;
	}
	var code = $("input[id='addcode']").val();
	if (!code) {
		alert('参数代码必须填写!');
		return;
	}
	var descr = $("input[id='adddescr']").val();
	if (!descr) {
		alert('描述信息必须填写!');
		return;
	}
	var valuesets = $("input[id='addvaluesets']").val();
	if (!valuesets) {
		alert('请输入参数值集!');
		return;
	}
	var radios = document.getElementsByName("value");
	var result = false;
	for(var i=0;i<radios .length;i++){
		if(radios[i].checked){
	     	result=true;
	   	}
	}
	if(!result){
	   alert("请至少选择一个参数值!");
	   return;
	}
	art.dialog.post({
		url: "/config/ajax_save.do",
        data:$("#addform").serialize(),
        success: function(data){
            if(data.success){
                location.reload();
            }else{
            	alert(data.msg)
            }
        }
	},"加载中...");
	
	}
	
	//编辑提交
	function subedit(){
	[#--提交之前 form 要手动进行验证--]
	
	var name = $("input[id='editname']").val();
	if (!name) {
		alert('参数名称必须填写!');
		return;
	}
	var code = $("input[id='editcode']").val();
	if (!code) {
		alert('参数代码必须填写!');
		return;
	}
	var descr = $("input[id='editdescr']").val();
	if (!descr) {
		alert('描述信息必须填写!');
		return;
	}
	var valuesets = $("input[id='editvaluesets']").val();
	if (!valuesets) {
		alert('请输入参数值集!');
		return;
	}
	var radios = document.getElementsByName("value");
	var result = false;
	for(var i=0;i<radios .length;i++){
		if(radios[i].checked){
	     	result=true;
	   	}
	}
	if(!result){
	   alert("请至少选择一个参数值!");
	   return;
	}
	art.dialog.post({
		url: "/config/ajax_save.do",
        data:$("#editform").serialize(),
        success: function(data){
            if(data.success){
                location.reload();
            }else{
            	alert(data.msg)
            }
        }
	},"加载中...");
	}
	
	

</script>